package com.zx.cloud.file;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

//@RestController
public class FileController {

	@Resource
	FileServerUtils fileUtils;

	
	@RequestMapping("/file/{file}")
	public void fileDown(@PathVariable("file") String fileName, HttpServletRequest request, HttpServletResponse response) {
		if (StringUtils.isEmpty(fileName)) {
			return;
		}
		StringBuffer sbn = new StringBuffer();
		sbn.append(fileUtils.getLocalDir()).append(fileName);
		String dirFile = sbn.toString();
		String fileType = fileName.substring(fileName.indexOf('.') + 1, fileName.length());
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		response.addHeader("Cache-Control", "post-check=0, pre-check=0");
		response.setHeader("Pragma", "no-cache");
		response.setContentType("file/" + fileType);
		try {
			ServletOutputStream out = response.getOutputStream();

			File file = new File(dirFile);
			if (!file.exists()) {
				return;
			}
			FileInputStream in = new FileInputStream(file);
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = in.read(buffer)) != -1) {
				out.write(buffer, 0, len);
			}
			in.close();
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}
	}

	@RequestMapping("/pic/{image}")
	public void image(@PathVariable("image") String fileName, HttpServletRequest request, HttpServletResponse response) {
		if (StringUtils.isEmpty(fileName)) {
			return;
		}
		StringBuffer sbn = new StringBuffer();
		sbn.append(fileUtils.getLocalDir()).append(fileName);
		String imgFile = sbn.toString();
		String imgType = fileName.substring(fileName.indexOf('.') + 1, fileName.length());
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		response.addHeader("Cache-Control", "post-check=0, pre-check=0");
		response.setHeader("Pragma", "no-cache");
		response.setContentType("image/" + imgType);
		try {
			BufferedImage bi = null;
			ServletOutputStream out = response.getOutputStream();

			File file = new File(imgFile);
			if (!file.exists()) {
				return;
			}
			bi = ImageIO.read(file);
			ImageIO.write(bi, imgType, out);

			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}
	}

	@RequestMapping(value = "/loadpic", method = RequestMethod.POST)
	public String saveImage(HttpServletRequest request, @RequestParam("file") MultipartFile file)
			throws InterruptedException {
		String fileName = "";
		try {
			if (file != null && !file.isEmpty()) {
				fileName = fileUtils.imageFileUpload(file);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fileName;
	}

	@RequestMapping(value = "/loadpics", method = RequestMethod.POST)
	public String saveImages(MultipartHttpServletRequest request) throws Exception {
		Iterator<String> fileNames = request.getFileNames();
		StringBuffer strFile = new StringBuffer();
		while (fileNames.hasNext()) {
			String fileName = fileNames.next();
			List<MultipartFile> fileList = request.getFiles(fileName);
			if (fileList.size() > 0) {
				Iterator<MultipartFile> fileIte = fileList.iterator();
				while (fileIte.hasNext()) {
					MultipartFile multipartFile = fileIte.next();
					String retName = fileUtils.imageFileUpload(multipartFile);
					if (!StringUtils.isEmpty(retName)) {
						strFile.append(retName).append(",");
					}
				}
			}
		}
		return strFile.toString();
	}
}
